/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-01-11 11:48:40
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-08-02 15:05:06
 * @FilePath: \cxl-h5\src\pages\login\base\index.ts
 */
import { getResetPasswordCode } from '@/apis/login'
import { getRegisterCode } from '@/apis/register'
import { CxlMessage } from '@/components/message'
import { CountDown } from '@/utils'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'

export class RegisterBaseInformationData {
    router = useRouter()
    //手机号码
    phone = ref('')
    //验证码
    code = ref('')
    //密码
    password = ref('')
    //再次确认密码
    passwordAgain = ref('')
    //倒计时是否结束
    isCountDownFinish = ref(true)
    //倒计时实例
    countDownInstance: CountDown
    //60秒倒计时
    second = 60
    //倒计时时间
    timeNumber = ref(this.second)
    //是否是注册
    isRegister = false
    //是否发送了验证码
    isSendSms = false
    //数据
    baseData = reactive({
        phone: '',
        code: '',
        password: '',
        passwordAgain: '',
        active: false
    })
    identical = ref(true)
    errorPasswordText = ref('')
    constructor() {
        this.countDownInstance = new CountDown(this.second)
    }

    resetBase = () => {
        this.phone.value = ''
        this.code.value = ''
        this.password.value = ''
        this.passwordAgain.value = ''
        this.isCountDownFinish.value = true
        this.isRegister = false
        this.isSendSms = false
        this.baseData.phone = ''
        this.baseData.code = ''
        this.baseData.password = ''
        this.baseData.passwordAgain = ''
        this.baseData.active = false
        this.identical.value = true
        this.errorPasswordText.value = ''
        console.log(100)
    }

    /**
     * 通过手机号获取注册验证码
     */

    onRegisterCode = (phoneRef: any) => {
        console.log(phoneRef)
        if (!phoneRef.validateInput()) {
            return false
        }

        if (this.isCountDownFinish.value) {
            this.timeNumber.value = this.second
            if (!this.isSendSms) {
                this.isSendSms = true
                this.getSmsCode().then(
                    (res: any) => {
                        console.log(res)
                        this.isCountDownFinish.value = false
                        this.isSendSms = false
                        CxlMessage.success('验证码已发送，请注意查收！')
                        this.countDownInstance.start(
                            n => {
                                this.timeNumber.value = n as number
                            },
                            () => {
                                this.isCountDownFinish.value = true
                            }
                        )
                    },
                    (desc: string) => {
                        this.isSendSms = false
                        CxlMessage.error(desc)
                    }
                )
            }
        }
    }

    getSmsCode = () => {
        console.log('isRegister', this.isRegister)
        return new Promise((resolve, reject) => {
            if (this.isRegister) {
                //注册发送短信
                getRegisterCode(this.baseData.phone).then((res: any) => {
                    if (res.data.value === 200) {
                        resolve(res.data.data)
                    } else {
                        reject(res.data.desc)
                    }
                })
            } else {
                //找回密码发送短信
                getResetPasswordCode(this.baseData.phone).then((res: any) => {
                    if (res.data.value === 200) {
                        resolve(res.data.data)
                    } else {
                        reject(res.data.desc)
                    }
                })
            }
        })
    }
    checkPassword = () => {
        // if (!checkPassword(this.baseData.password) && this.baseData.password.length) {
        //     // CxlMessage.error('您输入的密码不符合规则，请重新输入！')
        // }
    }
    /**
     * 确认密码输入框失去焦点时，校验两次输入的密码是否一致
     * @param value
     */
    checkPasswordEqually = () => {
        if (this.baseData.password.length && this.baseData.password !== this.baseData.passwordAgain) {
            // CxlMessage.error('两次输入的密码不一致，请重新输入！')
            this.errorPasswordText.value = '两次输入的密码不一致，请重新输入！'
            this.identical.value = false
            console.log(200)
        } else {
            this.errorPasswordText.value = ''
            this.identical.value = true
            console.log(300)
        }

        console.log('contarst', this.errorPasswordText.value)
    }
}
